package cn.wolfcode.wolf2world.web.controller;

import cn.wolfcode.wolf2world.domain.Region;
import cn.wolfcode.wolf2world.query.RegionQueryObject;
import cn.wolfcode.wolf2world.service.DestinationService;
import cn.wolfcode.wolf2world.service.RegionService;
import cn.wolfcode.wolf2world.vo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author 杨兴健
 * @Date 2020/8/9 20:54
 */
@Controller
@RequestMapping("/region")
public class RegionController {
    @Autowired
    private RegionService regionService;

    @Autowired
    private DestinationService destinationService;


    /**
     * 高级查询
     * @param model
     * @param qo 查询条件
     * @return
     */
    @RequestMapping("/list")
    public Object query(Model model, @ModelAttribute("qo") RegionQueryObject qo) {
        Page<Region> page = regionService.query(qo);
        model.addAttribute("page",page);
        return "region/list";
    }

    /**
     * 查询所有区域
     * @param response
     * @return
     */
    @GetMapping("/listDests")
    @ResponseBody
    public Object listDests(HttpServletResponse response) {
        response.setContentType("application/json;charset:utf-8");
        return destinationService.list();
    }

    /**
     * 新增或更新区域
     * @param region
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region,HttpServletResponse response) {
        regionService.saveOrUpdate(region);
        response.setContentType("application/json;charset:utf-8");
        return JsonResult.success();
    }

    /**
     * 根据区域id删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        regionService.delete(id);
        return JsonResult.success();
    }

    /**
     * 根据区域id查看关联目的地列表
     * @return
     */
    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid) {
        return destinationService.queryByRegionId(rid);
    }

    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(Integer hot,String id) {
        //全量跟新
        regionService.changeHotValue(hot,id);
        return JsonResult.success();
    }

}
